package com.ctvit.sjk.knowledge.app.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctvit.sjk.knowledge.app.mapper.TabAppMapper;
import com.ctvit.sjk.knowledge.app.pojo.domain.TabApp;
import com.ctvit.sjk.knowledge.app.pojo.domain.TabAppAccessToken;
import com.ctvit.sjk.knowledge.app.pojo.domain.TabAppDatasetMapping;
import com.ctvit.sjk.knowledge.app.pojo.dto.AppCreateDTO;
import com.ctvit.sjk.knowledge.app.pojo.dto.AppUpdateDTO;
import com.ctvit.sjk.knowledge.app.pojo.vo.AppGetVO;
import com.ctvit.sjk.knowledge.app.service.ITabAppAccessTokenService;
import com.ctvit.sjk.knowledge.app.service.ITabAppDatasetMappingService;
import com.ctvit.sjk.knowledge.app.service.ITabAppService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 应用Service业务层处理
 *
 * @author heyingcheng
 * @date 2025/3/22
 */
@Service
public class TabAppServiceImpl extends ServiceImpl<TabAppMapper, TabApp> implements ITabAppService {

    private final ITabAppAccessTokenService tabAppAccessTokenService;

    private final ITabAppDatasetMappingService tabAppDatasetMappingService;

    public TabAppServiceImpl(ITabAppAccessTokenService tabAppAccessTokenService, ITabAppDatasetMappingService tabAppDatasetMappingService) {
        this.tabAppAccessTokenService = tabAppAccessTokenService;
        this.tabAppDatasetMappingService = tabAppDatasetMappingService;
    }

    @Override
    public AppGetVO create(AppCreateDTO dto, String userId) {
        TabApp entity = dto.assige(userId);
        entity.setAppId(IdWorker.getIdStr());
        save(entity);

        TabAppAccessToken atEntity = new TabAppAccessToken();
        atEntity.setAppId(entity.getAppId());
        atEntity.setAccessToken(IdWorker.getIdStr());
        atEntity.setWhiteList(JSON.toJSONString(new JSONArray()));
        atEntity.setCreateId(userId);
        atEntity.setCreateTime(LocalDateTime.now());
        atEntity.setUpdateId(userId);
        atEntity.setUpdateTime(LocalDateTime.now());
        tabAppAccessTokenService.save(atEntity);

        return new AppGetVO(entity);
    }

    @Override
    public AppGetVO update(String appId, AppUpdateDTO dto, String userId) {
        TabApp entity = dto.assige(userId);
        entity.setAppId(appId);
        updateById(entity);
        return new AppGetVO(entity);
    }
    @Override
    public List<AppGetVO> listByDatasetId(String datasetId) {
        LambdaQueryWrapper<TabAppDatasetMapping> mappingWrapper = new LambdaQueryWrapper<>();
        mappingWrapper.select(TabAppDatasetMapping::getAppId);
        mappingWrapper.eq(TabAppDatasetMapping::getDatasetId, datasetId);
        List<String> appIdList = tabAppDatasetMappingService.list(mappingWrapper).stream().map(TabAppDatasetMapping::getAppId).collect(Collectors.toList());
        if (appIdList.isEmpty()) {
            return Collections.emptyList();
        }
        return listByIds(appIdList).stream().map(AppGetVO::new).collect(Collectors.toList());
    }

}
